﻿using System;
using System.Drawing;
using System.Threading;
using System.Windows;
using AutoLoL.Logic.Automation;
using Point = System.Drawing.Point;

namespace AutoLoL.Logic.Windows
{
    public class BaseWindow : IDisposable
    {
        private const int SW_SHOWDEFAULT = 10;

        public IntPtr Handle { get; set; }

        public bool IsForegroundWindow
        {
            get
            {
                IntPtr hWndForeground = Win32.GetForegroundWindow();
                if (hWndForeground == IntPtr.Zero || !Handle.Equals(hWndForeground))
                {
                    return false;
                }

                return true;
            }
        }

        public Color GetPixelColor(int x, int y)
        {
            return Win32.GetPixelColor(Handle, x, y);
        }

        public Point Center
        {
            get
            {
                int midX = (int)(ClientRectangle.Width / 2);
                int midY = (int)(ClientRectangle.Height / 2);
                Point center = new Point(midX, midY);
                center.Offset(Position);
                return center;
            }
        }

        public Point Position
        {
            get
            {
                Point windowPoint = new Point();
                Win32.ClientToScreen(Handle, ref windowPoint);
                return windowPoint;
            }
        }

        public Rect ClientRectangle
        {
            get
            {
                Rect windowRect = new Rect();
                Win32.GetWindowRectangle(Handle, ref windowRect);
                return windowRect;
            }
        }

        [Obsolete("Removed because the client redrawing sucks (but it seems to be fixed now?)")]
        public void SetSize(int width, int height)
        {
            Rect windowRect = ClientRectangle;
            Win32.ShowWindow(Handle, 0);
            Thread.Sleep(200);
            Win32.SetWindowPos(Handle, IntPtr.Zero, (int)windowRect.Left, (int)windowRect.Top, width, height, new uint());
            Thread.Sleep(200);
        }

        public void SetFocus()
        {
            Win32.ShowWindow(Handle, SW_SHOWDEFAULT);
            Thread.Sleep(200);
            Win32.SetForegroundWindow(Handle);
        }

        public void Dispose()
        {
            Handle = IntPtr.Zero;
        }
    }
}
